package org.galaxyworld.flycode.jobs;

import org.apache.commons.lang3.StringUtils;
import org.beetl.core.Template;
import org.galaxyworld.flycode.config.*;
import org.galaxyworld.flycode.core.TemplateManager;
import org.galaxyworld.flycode.utils.FileWriterUtils;
import org.galaxyworld.flycode.utils.NameUtils;

import java.io.Writer;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Model {

    private Template template;

    private String className;

    private String tableName;

    private List<Field> fields;

    private List<Field> primaryKeys;

    private List<ForeignKey> foreignKeys;

    private boolean hasDateType;

    private boolean hasKeywords;

    public Model() {
        AppConfigData appConfigData = ConfigManager.getConfig(AppConfigData.class);
        ModelConfigData modelConfigData = ConfigManager.getConfig(ModelConfigData.class);

        fields = new LinkedList<>();
        primaryKeys = new LinkedList<>();
        foreignKeys = new LinkedList<>();

        TemplateManager templateManager = TemplateManager.getInstance();
        template = templateManager.getTemplate(String.format("%s.tpl", modelConfigData.getTemplateName()));

        Map<String, Object> shared = new HashMap<>();
        shared.put("java8", appConfigData.isJava8Enabled());
        shared.put("packageName", modelConfigData.getPackageName());
        templateManager.setSharedVariables(shared);
    }

    public void addField(Field field) {
        fields.add(field);
    }

    public void addPrimaryKey(Field field) {
        primaryKeys.add(field);
    }

    public List<Field> getFields() {
        return fields;
    }

    public List<Field> getPrimaryKeys() {
        return primaryKeys;
    }

    public void addForeignKey(ForeignKey foreignKey) {
        foreignKeys.add(foreignKey);
    }

    public List<ForeignKey> getForeignKeys() {
        return foreignKeys;
    }

    public String getClassName() {
        if (StringUtils.isBlank(className)) {
            JdbcConfigData jdbcConfigData = ConfigManager.getConfig(JdbcConfigData.class);
            className = NameUtils.tableNameToClassName(tableName, jdbcConfigData.getPrefixList(), jdbcConfigData.isIncludePrefix());
        }
        return className;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public void setHasDateType(boolean hasDateType) {
        this.hasDateType = hasDateType;
    }

    public void setHasKeywords(boolean hasKeywords) {
        this.hasKeywords = hasKeywords;
    }

    public void saveToFile() {
        AppConfigData appConfigData = ConfigManager.getConfig(AppConfigData.class);
        ModelConfigData modelConfigData = ConfigManager.getConfig(ModelConfigData.class);

        template.binding("className", getClassName());
        template.binding("tableName", tableName);
        template.binding("fields", fields);
        template.binding("primaryKeys", primaryKeys);
//        template.binding("foreignKeys", foreignKeys);
        template.binding("hasDateType", hasDateType);
        template.binding("hasKeywords", hasKeywords);
        Writer writer = FileWriterUtils.createWriter(appConfigData.getOutputDirPath(), modelConfigData.getPackageName(), className);
        template.renderTo(writer);
    }
}
